package cn.heycm.blog.common.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;

/**
 * Convert Helper
 *
 * @author hey
 * @version 1.0
 * @since 2024/9/26 17:57
 */
public class ConvertHelper {

    private ConvertHelper() {}

    public static <T, R> List<R> list(Collection<T> collection, Function<T, R> function) {
        if (null == collection || collection.isEmpty()) {
            return Collections.emptyList();
        }
        List<R> list = new ArrayList<>(collection.size());
        collection.forEach(item -> list.add(function.apply(item)));
        return list;
    }

    public static <T, R> List<R> list(Collection<T> collection, BiFunction<T, Integer, R> function) {
        if (null == collection || collection.isEmpty()) {
            return Collections.emptyList();
        }
        List<R> list = new ArrayList<>(collection.size());
        int index = -1;
        for (T item : collection) {
            index++;
            list.add(function.apply(item, index));
        }
        return list;
    }
}
